#include <windows.h>
#include <stdio.h>
#include "ukonton.h"
#include "modules.h"

#define UKHDR_VER 0x10
#define PLUGIN_NAME "core"
#define PLUGIN_VERS "v0.1"

int core_init();
char *core_run(char* args);

ukontonModule module_core =
{
	UKHDR_VER,
	PLUGIN_NAME,
	PLUGIN_VERS,
	core_init,
	core_run,
};

int core_init(){
	return 0;
}

char *core_run(char* args){
	char *buf;
	char tmp[10];
	modType *modt=NULL;
	modulesNode **node=NULL;
	buf=malloc(sizeof(char)*512+1);

	if(!strncmp(args,"load:",5)){
		modt=modload(args+5);
		if(modt){
			(void)printf("MODULE: '%s' loaded!\n",args+5);
			list_add(&mods,modt);
			mods->mod->module->init();
			sprintf_s((char*)&tmp,10,"loaded!");
		}else{
		(void)printf("WARNING: Can't load module '%s'\n",args+5);
		sprintf_s((char*)&tmp,10,"error!");
		}
	}else if(!strncmp(args,"unload:",7)){
		node=list_search(&mods,args+7);
		if(node){
			if((*node)->mod->lib){
				FreeLibrary((*node)->mod->lib);
				list_remove(node);
				(void)printf("WARNING: unloaded module '%s'\n",args+7);
				sprintf_s((char*)&tmp,10,"cleared!");
			}else{
				(void)printf("WARNING: unloading module '%s' not possible, because it's compiled in!\n",args+7);
				sprintf_s((char*)&tmp,10,"static!");
			}
		}else{
			(void)printf("WARNING: unloading module '%s' failed!\n",args+7);
			sprintf_s((char*)&tmp,10,"error!");
		}
	}
	sprintf_s(buf,512,"\
<html>\
	<head>\
		<title>uKonton</title>\
	<head>\
	<body>\
		<h1>ukonton</h1><hr>\
		%s\
	</body>\
</html>\
	",tmp);
	return buf;
}
